# coding: utf-8

from django.db import models


class Countries(models.Model):
    u' Список стран '
    country_id = models.IntegerField(unique=True)
    country_name = models.CharField(max_length=150, blank=True)
    added = models.DateTimeField(null=True, blank=True)

    class Meta:
        db_table = u'en_countries'

    def __unicode__(self):
        return self.country_name


class Regions(models.Model):
    u' регионы '
    country = models.ForeignKey(Countries, verbose_name=u'страна')
    region_id = models.IntegerField(unique=True)
    region_name = models.CharField(max_length=300, blank=True, verbose_name=u'регион')
    players = models.IntegerField(null=True, blank=True, verbose_name=u'количество игроков')
    games_played = models.IntegerField(null=True, blank=True, verbose_name=u'сыграно игр')
    teams = models.IntegerField(null=True, blank=True, verbose_name=u'количество команд')
    added = models.DateTimeField(null=True, blank=True, verbose_name=u'дата добавления')

    class Meta:
        db_table = u'en_regions'

    def __unicode__(self):
        return u'%s (%s)' % (self.region_name, self.country)


class Cities(models.Model):
    u' города '
    city_name = models.CharField(unique=True, max_length=250, verbose_name=u'город')
    lat = models.FloatField(null=True, blank=True)
    lng = models.FloatField(null=True, blank=True)
    country = models.ForeignKey(Countries, verbose_name=u'страна')

    class Meta:
        db_table = u'en_cities'
        verbose_name = u'город'
        verbose_name_plural = u'города'

    def __unicode__(self):
        return u'%s (%s)' % (self.city_name, self.country,)


class Domains(models.Model):
    u' Игровые домены Encounter '
    domain_name = models.CharField(unique=True, max_length=150, verbose_name=u'название')
    city_name = models.CharField(max_length=450, blank=True, verbose_name=u'город')
    owner_id = models.IntegerField(null=True, blank=True, verbose_name=u'ID владельца')
    owner_name = models.CharField(max_length=600, blank=True, verbose_name=u'владелец домена')
    games_played = models.IntegerField(null=True, blank=True, verbose_name=u'сыграно игр')
    players = models.IntegerField(null=True, blank=True, verbose_name=u'игроков в домене')
    teams = models.IntegerField(null=True, blank=True, verbose_name=u'команд в домене')
    country = models.ForeignKey(Countries, verbose_name=u'страна')
    region = models.ForeignKey(Regions,null=True, blank=True, verbose_name=u'регион')
    added = models.DateTimeField(null=True, blank=True)
    updated = models.DateTimeField(null=True, blank=True)
    disable_scenarios = models.BooleanField(default=False, verbose_name=u'Скрыть все сценарии домена')

    class Meta:
        db_table = u'en_domains'
        verbose_name = u'игровой домен'
        verbose_name_plural = u'игровые домены'

    def __unicode__(self):
        return self.domain_name

class Games(models.Model):
    u' Игры доменов '
    domain = models.ForeignKey(Domains)
    game_name = models.CharField(max_length=700, blank=True)
    game_date_str = models.CharField(max_length=90, blank=True)
    game_date = models.DateField(blank=True)
    game_num = models.IntegerField(null=True, blank=True)
    has_scenario = models.IntegerField(null=True, blank=True, default=-1, verbose_name=u'Имеется ли сценарий в исходнике?')
    game_type = models.CharField(max_length=90, blank=True)
    game_zone = models.CharField(max_length=90, blank=True)
    added = models.DateTimeField(null=True, blank=True)
    scenario_updated = models.DateTimeField(null=True, blank=True)
    scenario_parsed = models.IntegerField(null=True, blank=True)
    disable_scenario = models.BooleanField(default=False, verbose_name=u'Закрыть сценарий')

    class Meta:
        db_table = u'enlab_games'
        verbose_name = u'игра'
        verbose_name_plural = u'игры'

    def __unicode__(self):
        return u'%s (#%d) / %s' % (self.game_name, self.game_num, self.domain.domain_name, )

    def gamescenario_url(self):
        return "http://%s/GameScenario.aspx?gid=%d" % (domain.domain_name,id,)

    def can_show_scenario(self):
        u' Признак, можем ли показать сценарий '
        if self.has_scenario and not self.disable_scenario and not self.domain.disable_scenarios:
            return True
        return False